package com.plsjavaweb.ui.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.plsjavaweb.core.EmployeeSeatCore;
import com.plsjavaweb.core.SmartSearchCore;
import com.plsjavaweb.pojo.EmployeeSeatPOJO;

/**
 * Servlet implementation class SmartSearch
 */
@WebServlet("/SmartSearch")
public class SmartSearch extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SmartSearch() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String searchString = (String) request.getParameter("search");
		SmartSearchCore coreSearch = new SmartSearchCore();
		EmployeeSeatCore seatCore = new EmployeeSeatCore();
		String locationCode = "PIC";
		String floor = "3F";
		List<EmployeeSeatPOJO> employeeSeats = seatCore.getEmployeeSeatsByBldgByFloor(locationCode, floor);
		List<EmployeeSeatPOJO> empSeats = new ArrayList<EmployeeSeatPOJO>();
		
		if (!searchString.trim().isEmpty()) {
			empSeats = coreSearch.searchWildCard(searchString);
		} 
		request.setAttribute("searchString", searchString);
		request.setAttribute("empSeats", empSeats);
		request.setAttribute("employeeSeats", employeeSeats);
		RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/search-result.jsp");
		rd.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
